# -*-encoding=utf-8-*-
__author__ = '帆'

import threading, time, CreateTags, string

class TagsThread(threading.Thread):
    def __init__(self, queue, obj, db):
        self.queue = queue
        self.thread = threading.Thread.__init__(self)
        self.isStop = False
        self.obj = obj
        self.db = db

    def run(self):
        while not self.isStop:
            if self.queue.qsize() == 0:
                print('over')
                self.isStop = True
                break

            cell = self.queue.get()
            cellId = cell[0]
            location = [str(cell[2]), str(cell[1])]
            # print(location)
            keyworkds = ['医院', '学校', '地铁', '超市']
            # keyworkds = ['学校']
            cellTagId = []
            for k in keyworkds:
                result = self.obj.requestUrl(location, [k])
                if result:
                    cellTagId.append(self.getCellId(k))
            tagId = list(set(cellTagId))
            # if not tagId: continue
            tagId = string.join(tagId, ',')
            # print(type(tagId))
            self.db.updateCellTag(cellId, tagId)
            time.sleep(0.1)

    def getCellId(self, keywords):
        tags = self.db.getCellTags()
        # print(tags)
        # print(type(unicode('中文')))
        tagId = None
        if keywords == '医院':
            tagId = tags.get('生活便利')
        elif keywords == '学校':
            tagId = tags.get('名校学位')
        elif keywords == '地铁':
            tagId = tags.get('地铁旁')
        elif keywords == '超市':
            tagId = tags.get('生活便利')
        # print(tagId)
        return str(tagId)